/**
 * 
 */
import java.io.IOException;
import java.sql.Connection;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Action.AutoNews;
import Action.Clip;
import Action.Comment;
import Action.NewsFromArt;

import Action.News;
import Action.Topic;
import Action.UserManagement;
import Login.CheckLogin;
import Utils.Common;
import Utils.Constant;
import Utils.DbAccess;

import Utils.ZipResponse;









public class DoAdmin extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 4194452284080389577L;
	
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(Constant.DB,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.setContentType("text/xml");
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
		String action = req.getParameter("action");
		if(!CheckLogin.check(req)){
			ZipResponse.write(req, res, "notlogin", "UTF-8");
			return;
		}
		String userid = req.getSession().getAttribute("userid").toString();
		String type = req.getSession().getAttribute("usertype").toString();
		String xml="";
		Connection conn = null;
		try{
			conn = db.getConn();
			
			if(action.equals("listAutoNews")){
				xml = new AutoNews().getNewsAPI(req, res,conn);
			}else if(action.equals("addAutoNews")){
				xml = new AutoNews().insertAutoNewsList(req, res,userid, conn);
			}else if(action.equals("listNews")){
				xml = new News().getListNews(req, conn,userid,type);
			}else if(action.equals("getNewsDetail")){
				xml = new News().getNewsDetail(req,userid,type ,conn);
			}else if(action.equals("deleteFileImg")){
				xml = new News().deleteImg(req, conn);
			}else if(action.equals("updateThumb")){
				xml = new News().updateThumbnail(req, conn,userid,type);
			}else if(action.equals("deleteHomePic")){
				xml = new News().deleteHomePic(req, conn);
			}else if(action.equals("deleteNews")){
				xml = new News().deleteNews(req, conn,userid,type);
			}else if(action.equals("updateStatus")){
				xml = new News().updateStatus(req, conn,userid,type);
			}else if(action.equals("listHome")){
				xml = new News().listHome(req.getParameter("catId"),conn);
			}else if(action.equals("deleteHome")){
				xml = new News().deleteHome(req, conn);
			}else if(action.equals("updatePosition")){
				xml = new News().updatePosition(req, conn);
			}else if(action.equals("publishHome")){
				String catId = req.getParameter("catId");
				if(new News().publishHome(catId, conn)) 	
					xml = Common.makeSuccessXML();
				else xml = Common.makeErrorXML("Error");
			}else if(action.equals("listNewsCat")){
				xml = new News().listNewsCat(req, conn);
			}else if(action.equals("addHome")){
				xml = new News().insertHome(req,userid, conn);
			}else if(action.equals("getClip")){
				xml = new Clip().listClip(conn);
			}else if(action.equals("clipDetail")){
				xml = new Clip().clipDetail(req, conn);
			}else if(action.equals("deleteClip")){
				xml = new Clip().deleteClip(req, conn);
			}else if(action.equals("updateStatusTopic")){
				xml = new Topic().updateStatusTopic(req, conn); 
			}else if(action.equals("listUser")){
				xml = new UserManagement().getUserList(conn);
			}else if(action.equals("viewSubmited")){
				String useridS = req.getParameter("userId");
				xml = new UserManagement().getUserSubmited(useridS, conn);
			}else if(action.equals("clearAllCache")){
				try{conn.close();}catch (Exception e){}
				new News().clearAllCache();
				xml = Common.makeSuccessXML();
			}else if(action.equals("makeContentEmail")){
				xml = new News().genContentForEmail(req, conn);
			}else if(action.equals("listComment")){
				xml = new Comment().load(req, conn);
			}else if(action.equals("deleteComment")){
				xml = new Comment().delete(req, conn);
			}else if(action.equals("listNewsInGroup")){
				String groupId = req.getParameter("groupId");
				xml = new News().listNewsInGroup(groupId, conn);
			}else if(action.equals("addNewsToGroup")){
				xml = new News().addNewsToGroup(req, conn);
			}else if(action.equals("deleteNewsInGroup")){
				String newsId = req.getParameter("newsId");
				String groupId = req.getParameter("groupId");
				xml = new News().deleteNewsInGroup(newsId, groupId, conn);
			}else if(action.equals("updateNewsInGroup")){
				xml = new News().updateNewsInGroup(req, conn);
			}else if(action.equals("changeTopToLatest")){
				xml = new News().changeTopNewsToLatest(req, conn);
			}else if(action.equals("getNewsFromArt")){
				xml = new NewsFromArt().getAllNews(conn);
			}else if(action.equals("resetDatePublished")){
				xml = new News().resetDatePublished(conn);
			}else if(action.equals("updatenews")){
				xml = new AutoNews().updatenews(req, res, conn);
			}else if(action.equals("updatenewsshare")){
				xml = new AutoNews().updatenewsshare(req, res, conn);
			}else if(action.equals("listUserShare")){
				xml = new AutoNews().listUserShare(req, res, conn);
			}
			
			ZipResponse.write(req, res, xml, "UTF-8");
		}catch (Exception e) {
			
		}finally{
			if(conn!=null){try{conn.close();}catch (Exception e) {
			}}
		}

	}
	
}
